// src/store/slices/authSlice.ts
import { createSlice, PayloadAction, Draft } from "@reduxjs/toolkit";
import { AppState } from "@store/index";
import { persistReducer } from "redux-persist";
import storage from "redux-persist/lib/storage";
// 状态切片中的状态的类型
export interface AuthState {
    auth: Partial<Auth>;
}

// 创建用于管理用户登录凭据的状态切片
export const authSlice = createSlice<
    AuthState,
    {
        // 保存用户登录凭据
        saveAuth(state: Draft<AuthState>, action: PayloadAction<Auth>): void;
        // 清空用户登录凭据
        clearAuth(state: Draft<AuthState>): void;
    },
    "auth"
    >({
    name: "auth",
    initialState: {
        auth: {},
    },
    reducers: {
        // 保存用户登录凭据
        saveAuth(state, action) {
            state.auth = action.payload;
        },
        // 清空用户登录凭据
        clearAuth(state) {
            state.auth = {};
        },
    },
});
// 持久化
export const authReducer = persistReducer(
    { key: authSlice.name, storage },
    authSlice.reducer
);
// 导出用于操作用户资料的 action creator 函数的对象集合
export const { saveAuth, clearAuth } = authSlice.actions;
// 导出用于获取用户资料的状态选择器
export const selectAuth = (state: AppState) => state.auth.auth;